import Variables from '../others/variables';
import { Lua } from './lua_generator';

export const variables_get = function () {
    // Variable getter.
    var code = Lua.variableDB_.getName(this.getFieldValue('VAR'),
        Variables.NAME_TYPE) || '_';
    return [code, Lua.ORDER_ATOMIC];
};

// export const variables_declare = function() {
//   var dropdown_type = this.getFieldValue('TYPE');
//   var argument0;
//   //TODO: settype to variable
//   argument0 = Lua.valueToCode(this, 'VALUE',Lua.ORDER_ASSIGNMENT) ||  'nil';
//   var varName = Lua.variableDB_.getName(this.getFieldValue('VAR'),
//       Variables.NAME_TYPE);

//   if (dropdown_type === 'number')
//       Lua.definitions_['var_declare' + varName] = 'let ' + ' ' + varName + ' = 0;';
//   else if(dropdown_type === 'string')
//       Lua.definitions_['var_declare' + varName] = 'let ' + ' ' + varName + ' = \'\';';
//   else if(dropdown_type === 'boolean')
//       Lua.definitions_['var_declare' + varName] = 'let ' + ' ' + varName + ' = true;';
//   else if(dropdown_type.startsWith('Array'))
//       Lua.definitions_['var_declare' + varName] = 'let ' + varName + ':' + dropdown_type + ' = [];';


//   if(Lua.setups_['var_declare' + varName] === undefined) {
//       Lua.setups_['var_declare' + varName] =  varName + ' = ' + argument0 + '\n';
//   }else {
//   }
//   return '';
// };

export const variables_set = function () {
    // Variable setter.
    if (this.getFieldValue('VAR') == "") {
        return "  = nil\n";
    }
    var argument0 = Lua.valueToCode(this, 'VALUE',
        Lua.ORDER_ASSIGNMENT) || 'nil';
    var varName = Lua.variableDB_.getName(this.getFieldValue('VAR'), Variables.NAME_TYPE) || '_';
    return varName + ' = ' + argument0 + '\n';
};

export const variables_change = function () {
    // Variable setter.
    var operator = this.getFieldValue('OP');
    var varName = Lua.valueToCode(this, 'MYVALUE', Lua.ORDER_ATOMIC) || 'nil';
    var code = operator + '(' + varName + ')';
    return [code, Lua.ORDER_ATOMIC];
};

export const variables_global = function () {
    var str = Lua.valueToCode(this, 'VAR', Lua.ORDER_ATOMIC) || 'nil';
    var code = "global " + str + '\n';
    return code;
};

export const controls_type = function () {
    var data = Lua.valueToCode(this, 'DATA', Lua.ORDER_ATOMIC) || 'nil'
    var code = 'type(' + data + ')';
    return [code, Lua.ORDER_ATOMIC];
};

export const controls_typeLists = function () {
    //Lua.definitions_['import_microbit_*'] = 'from microbit import *';
    var type = this.getFieldValue('type');
    // Lua.definitions_['func_type' + type] = code;
    return [type, Lua.ORDER_ATOMIC];
}